package com.asj.vivo.lock.service.recover.correct;

import static com.asj.enums.DeviceIdentifierDictEnums.json_key_deviceCode;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_disabledAppInfo;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_restrictSettingInfo;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_result;
import static com.asj.enums.HttpRequestDictEnums.exchangeDeviceRestrictUrl;
import static com.asj.enums.ResultCodeEnums.result_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.service.command.specific.SpecificFunctionRestrictService;
import com.asj.service.command.specific.SpecificRestrictAppBlackService;
import com.asj.service.oppoSdk.OPPOSDKService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.Arrays;

/**
 * 矫正设备限制信息
 * */
public class CorrectDeviceRestrictInfo {

    /**
     * 核对与后台限制项，禁用项，是否一致，避免因为网络或其他原因导致限制项不生效
     * */
    public static void verifyRestrictItem(Context context) throws Exception {
        //获取设备IMEI
        OPPOSDKService opposdkService = new OPPOSDKService(context);
        String deviceCode = CommonsUtils.getDeviceCode(opposdkService.getIMEI());
        //封装请求参数
        JsonObject requestParam = new JsonObject();
        requestParam.addProperty(json_key_deviceCode.getValue(),deviceCode);

        //从后台获取后台的限制项
        String result = HttpRequestUtils.retryHttpRequest(exchangeDeviceRestrictUrl.getValue(), requestParam.toString(), result_success.getCode(), 5, context);
        if(!result_error.getMessage().equals(result)){
            //转换json数据
            JsonObject responseObj = JsonParser.parseString(result).getAsJsonObject();
            JsonObject restrictSettingItem = JsonParser.parseString(responseObj.get(json_key_result.getValue()).getAsString()).getAsJsonObject();
            if(!restrictSettingItem.isJsonNull()){
                //限制的app选项
                JsonElement restrictDisabledAppPackageNameElement = restrictSettingItem.get(json_key_disabledAppInfo.getValue());
                if(restrictDisabledAppPackageNameElement != null && !restrictDisabledAppPackageNameElement.isJsonNull()){
                    //限制的app包名
                    String restrictDisabledAppPackageName = restrictSettingItem.get(json_key_disabledAppInfo.getValue()).getAsString();
                    //将包名设入黑名单
                    Log.w("设备APP限制矫正","存在矫正，限制应用为："+restrictDisabledAppPackageName);
                    //设置禁用app
                    SpecificRestrictAppBlackService.setOnlyBlackList(context, Arrays.asList(restrictDisabledAppPackageName.split(",")));
                }
                //限制的功能项
                JsonElement restrictSettingInfoElement = restrictSettingItem.get(json_key_restrictSettingInfo.getValue());
                if(restrictSettingInfoElement != null && !restrictSettingInfoElement.isJsonNull()){
                    JsonObject restrictSettingInfo = restrictSettingItem.get(json_key_restrictSettingInfo.getValue()).getAsJsonObject();
                    Log.w("设备功能限制矫正","存在矫正，限制项为："+restrictSettingInfo);
                    SpecificFunctionRestrictService.operatingCenter(restrictSettingInfo,context);
                }
            }
        }
    }
}
